package com.wx.disparent.mallparent.service;

import com.wx.disparent.mallparent.commons.inface.IBaseService;
import com.wx.disparent.mallparent.dao.AddressMapper;
import com.wx.disparent.mallparent.dao.ProductMapper;
import com.wx.disparent.mallparent.entity.AddressEntity;
import com.wx.disparent.mallparent.entity.OrderEntity;
import com.wx.disparent.mallparent.entity.ProductEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 曾右权
 * @description
 * @date 2019/12/9 19:11
 */

@Service
public class AddressService implements IBaseService<AddressEntity> {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询所有地址（如果params为空，则查询所有地址。）
     * @param params 查询条件（根据用户id查询）
     * @return List<AddressEntity> 地址信息集合
     */
    public List<AddressEntity> findAll(Map<String, Object> params) {
        System.out.println(params);
        return addressMapper.findAll(params);
    }

    /**
     * 通过id查询地址信息
     * @param t id
     * @return AddressEntity 地址信息
     */
    public AddressEntity findById(int t) {
        return addressMapper.findById(t);
    }

    public AddressEntity findById(String t) {
        AddressEntity addressEntity;
        if (t != null && !t.equals("")){
            addressEntity = addressMapper.findById(Integer.parseInt(t));
        }else {
            addressEntity=null;
        }
        return addressEntity;
    }


    /**
     * 添加地址
     * @param addressEntity 地址信息
     * @return int 0 失败
     */
    public int insert(AddressEntity addressEntity) {
        int i;
//        地址创建时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sdf.format(date);
        addressEntity.setUpdateTime(createTime);
        addressEntity.setIsDelete("0");
        i = addressMapper.insert(addressEntity);
        return i;
    }

    /**
     * 修改
     * @param addressEntity 地址
     * @return 修改结果 0失败
     */
    @Override
    public int update(AddressEntity addressEntity) {
        int i;
        if (addressEntity.getId()!=null&&!addressEntity.getId().toString().equals("")){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String updateTime = sdf.format(date);
            addressEntity.setUpdateTime(updateTime);
            i= addressMapper.update(addressEntity);
        }else {
            i=0;
        }
      return i;
    }


    /**
     * 删除地址（软删除）
     * @param  t 地址id
     * @return int 0 失败
     */
    public int deleteById(int t) {
        return addressMapper.deleteById(t);
    }

}
